package cn.wizzer.app.cms.modules.services.impl;

import cn.wizzer.app.cms.modules.models.CmsVideo;
import cn.wizzer.app.cms.modules.services.CmsVideoService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.base.service.CommonDBService;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.plugins.wkcache.annotation.CacheDefaults;
import org.nutz.plugins.wkcache.annotation.CacheRemoveAll;
import org.nutz.plugins.wkcache.annotation.CacheResult;

@IocBean(args = {"refer:dao"})
@Service(interfaceClass = CmsVideoService.class)
@CacheDefaults(cacheName = "cms_video")
public class CmsVideoServiceImpl extends BaseServiceImpl<CmsVideo> implements CmsVideoService {
    public CmsVideoServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private CommonDBService dbService;


    @CacheResult
    public Pagination getListPage(int pageNumber, int pageSize, Condition cnd) {
        Sql sql = Sqls.create("select id,title,picUrl,times from cms_video $condition ");
        sql.setCondition(cnd);
        Sql countSql = Sqls.create("select count(1) from cms_video $condition ");
        countSql.setCondition(cnd);
        return dbService.listPage(CmsVideo.class, pageNumber, pageSize, sql.toString(), countSql.toString());
    }

    @CacheResult
    public CmsVideo getVideo(Condition cnd) {
        return this.fetch(cnd);
    }

    @CacheRemoveAll
    public void clearCache() {

    }

    @Override
    public void addPlayTime(String id) {
        CmsVideo fetch = this.fetch(id);
        fetch.setTimes(fetch.getTimes() + 1);
        this.updateIgnoreNull(fetch);
        clearCache();
    }

    @Override
    public void add(CmsVideo cmsVideo) {
        this.insert(cmsVideo);
        this.edit(cmsVideo);
    }

    public void edit(CmsVideo cmsVideo) {
        cmsVideo.setUrl(cmsVideo.getUrl() + "?id=" + cmsVideo.getId());
        this.update(cmsVideo);
        this.clearCache();
    }
}
